// ===============================================
// splash_page
//
// Create by Will on 2020/10/5 5:14 PM
// Copyright @flutter_base.All rights reserved.
// ===============================================

import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/res.dart';
import 'package:flutter_base/route/router.gr.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    debugPrint('$this createState()');
    return SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  static const _splashDuration = const Duration(seconds: 3);

  Timer _splashTimer;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      debugPrint('$this first frame callback');
      _loadFromPreferences();
    });
  }

  @override
  void dispose() {
    _splashTimer?.cancel();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      backgroundColor: CupertinoColors.white,
      child: Stack(
        children: <Widget>[
          FractionallySizedBox(
              widthFactor:1,
              heightFactor: 1,
              // child: HeyTeaStyle.image(
              //   image: HeyTeaStyle.assetImageProvider(Res.ic_heytea),
              // ),
              // child: Image.asset("assets/images/ic_splash.jpg") ,
              child: ExtendedImage(
                image: ExtendedAssetImageProvider(Res.ic_splash),
                fit: BoxFit.cover,
              )),
          // Container(
          //   // child: _buildChildWidget(),
          //   width: double.maxFinite,
          //   height: double.maxFinite,
          //   decoration: BoxDecoration(
          //     image: DecorationImage(
          //       image: ExtendedAssetImageProvider(Res.ic_splash),
          //       fit: BoxFit.cover,
          //     ),
          // ),
          // ),
        ],
      ),
    );
  }

  _loadFromPreferences() async {
    // 读取用户配置
    // final user = await SugarParkPreferences.user;
    // if (user != null) {
    //   final userProvider =
    //   Provider.of<SugarParkUserProvider>(context, listen: false);
    //   userProvider.value = user;
    // }
    // 读取应用配置
    // final preferences = await SugarParkPreferences.app;
    // if (preferences.isFirstPolicyAgreed == true) {
    //   _splashTimer = Timer(_splashDuration, _enterHomePage);
    // } else {
    //   _showFirstPolicyDialog();
    // }
    _splashTimer = Timer(_splashDuration, _enterHomePage);
  }

  _enterHomePage() async {
    debugPrint('$this Now go to the home page');
//    ExtendedNavigator.of(context).pushConversationPage(conversationId: '1287698903125618689');
    ExtendedNavigator.of(context).popAndPush(Routes.appMainPage);
//    ExtendedNavigator.of(context).popAndPush(Routes.conversationDetailPage);
//    final assetIds = [
//      'https://img95.699pic.com/photo/40100/6015.jpg_wh300.jpg',
//      'https://img95.699pic.com/photo/50046/5562.jpg_wh300.jpg',
//      'https://img95.699pic.com/photo/50055/5642.jpg_wh300.jpg',
//      // 有问题的Url，用来测试错误处理逻辑
//      'Load failed',
//    ];
//    ExtendedNavigator.of(context).pop();
//    ExtendedNavigator.of(context).pushAssetReaderPage(
//      assetIds: assetIds,
//      assetReadingTypes: {
//        assetIds[0]: AssetReadingType.normal,
//        assetIds[1]: AssetReadingType.redEnvelope,
//        assetIds[2]: AssetReadingType.burnAfterReading,
//        assetIds[3]: AssetReadingType.burnAfterReading,
//      },
//      assetPrices: {
//        assetIds[0]: 1,
//        assetIds[1]: 50,
//        assetIds[2]: 100,
//        assetIds[3]: 150,
//      },
//      assetThumbnails: {
//        assetIds[0]:
//            'https://upfiles.b0.upaiyun.com/support/third/tupianchuli/helpex3.2.jpg',
//        assetIds[1]:
//            'https://upfiles.b0.upaiyun.com/support/third/tupianchuli/helpex3.2.jpg',
//        assetIds[2]:
//            'https://upfiles.b0.upaiyun.com/support/third/tupianchuli/helpex3.2.jpg',
//        assetIds[3]:
//            'https://upfiles.b0.upaiyun.com/support/third/tupianchuli/helpex3.2.jpg',
//      },
//    );
  }
}
